package com.example.httpservlet;

//HTTP协议请求的Servlet

import com.sun.net.httpserver.HttpsServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/httpServletExample")
public class HttpServletExample extends HttpServlet{


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       System.out.println("HttpServletExample doGet() 执行~~");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HttpServletExample doPost() 执行~~");
        //解决页面显示中文乱码的问题
        resp.setContentType("text/html;charset=utf-8") ;
        //获取请求的参数值
        String username = req.getParameter("username");
        //通过响应实例resp获取PrintWriter对象，用于响应输出到网页页面
        PrintWriter writer = resp.getWriter();
        //输出到浏览器页面
        writer.write("\r\n");
        writer.write("\r\n");
        writer.write("<html>\r\n");
        writer.write("<head>\r\n");
        writer.write("    <title>Title</title>\r\n");
        writer.write("</head>\r\n");
        writer.write("<body>\r\n");
        writer.write("<h1>"+username+",Login Success"+"<h1>");
        writer.write("\r\n");
        writer.write("</body>\r\n");
        writer.write("</html>\r\n");
    }
}
